<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * 检查用户是否存在
     * @author  KevinBai
     * @param   string  $name   用户名
     * @param   int     $type   类型(0:用户名,1:邮箱)
     * @return  boolean
     */
    public function check_user($username)
    {
        //验证用户名是否存在
        $this->db->where("username",$username);
        $res = $this->db->get('tr_admin_user');
        if($res->num_rows()==0)
        {
            return FALSE;
        }
        return TRUE;
    }  

    public function editUser($AccountID = 0, $data = [])
    {
        if($AccountID){
             //修改
            $this->db->where("AccountID",$AccountID);
            $res = $this->db->update("tr_user", $data);
        }else{
            $res = $this->db->insert("tr_user", $data);
        }
        return $res;
    } 
    /**
     * 检查邮箱是否存在
     * @author  KevinBai
     * @param   string  $name   用户名
     * @param   int     $type   类型(0:用户名,1:邮箱)
     * @return  boolean
     */
    public function check_email($email)
    {
        //验证用户名是否存在
        $this->db->where("email",$email);
        $res = $this->db->get('tr_admin_user');
        if($res->num_rows()==0)
        {
            return FALSE;
        }
        return TRUE;
    } 

    /**
     * 重置密码
     * @param int $uid
     * @return string|boolean
     */
    function reset_password($AccountID,$newpassword) {
        $newpassword = md6($newpassword);
        $sql="update tr_user set password='{$newpassword}' where AccountID={$AccountID}";
        return $this->db->query($sql);
    }
   
    
     /**
     * 根据uid获取用户信息
     */
    public function get_userinfo($uid, $login_name)
    {
        if (!empty($uid))
        {
            $this->db->where('AccountID',$uid);
        }
        if (!empty($login_name))
        {
            if(stripos($login_name,"@")!==FALSE)
                {
                    $this->db->where('email',$login_name);
                }else{
                    $this->db->where('username',$login_name);
                }
        }
        $query = $this->db->get('tr_user');
        return $query->row_array();
    }

    /**
     * 检测登陆状态
     *
     * @return boolean
     */
    public function get_login_status()
    {
        $is_login = FALSE;
        $userId = isset($_SESSION['userInfo']['AccountID'])?$_SESSION['userInfo']['AccountID']:"";
        if (!empty($userId)){
            $is_login = TRUE;
        }
        return $is_login;
    }

    /**
     * 检测登陆账号
     *
     * @param string $username
     * @param string $password
     * @return array
     */
    public function check_account($username='', $password='')
    {
        $arr_res = array();

        $username = trim(addslashes($username));
        $password = trim(addslashes($password));

        $password = md6($password);
        $where = " ";
        $sql  = "SELECT * from tr_user  where isDel=0 and username = '{$username}' and password = '{$password}'";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0){
            $row = $query->row_array();
            if ($row['status'] != 1){
                $arr_res = array('status'=>1, 'message'=>'用户被禁用');
            }else{
                //获取用户权限
                //将用户信息写入session
                session_start();
                $_SESSION['userInfo'] = $row;
                //更新用户登陆日志
                $login_info = array(
                        'lastlogin'=>time(),
                        'lastip'=>$this->input->ip_address()
                );
                $this->db->where('AccountID', $row['AccountID']);
                $this->db->update('tr_user', $login_info);
                $arr_res = array('status'=>0, 'message'=>'','id'=>$row['AccountID']);
            }
        }else{
            $arr_res = array('status'=>2, 'message'=>'用户名或密码错误');
        }
        return $arr_res;
    }
    public function getDepartmentInfo($departmentid){
        if(empty($departmentid)){
            return false;
        }
        $this->db->where("id",$departmentid);
        $query = $this->db->get("tr_department");
        $result = $query->row_array();
        return $result['name'];
    }

    public function get_user_cnt($data = array()){
        $where = " WHERE 1 ";
        
        $sql = "SELECT
                    u.*,d.name department_name
            FROM
                   tr_user u,tr_department d  
            where 
                u.departmentid=d.id and u.status<>2 ";
        $query = $this->db->query($sql);
        return $query->num_rows();
    }
    
}

/* End of file School_user_model.php */
/* Location: ./application/models/school/school_user_model.php */